/**
 * Created by fusy on 2016/7/19.
 */
import React from 'react';
import util from 'util';
import {Collapse,Row, Col, Form, Input, Button, Checkbox, Radio, Tooltip, Icon,Table, Alert, InputNumber, Select, message} from 'antd';
import { RightButton  } from 'kpl';

const FormItem = Form.Item;
var Option = Select.Option;
var Panel = Collapse.Panel;

@Form.create() class InputPanel extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            rows:[],
            rowEnables:[],
            key:Math.random()
        };
    }

    componentDidMount() {

    }
    createOptionsByDict(dict) {
        var options = [];
        let list=util.getSysDict(dict);
        console.log("下拉框的值：",list);
        list.forEach((value, index) => {
            options.push(<Option key={dict + index} value={value.itemkey}>{value.itemval}</Option>)
        });
        return options;
    }
    callback(key) {
        console.log(key);
        if(key.length==0){
            return;
        }

        this.setState({
            rows:[],
        });


        // 绑定明细sdfafdq32
        util.httpRequest({
            type: "POST", url: 'param010201.json', params: {groupparaid:key[0]}, success: function (result) {
                if (result.ret_code === "0000") {
                    let enalble=[];

                    for (let i=0;i<result.rows.length;i++){
                        enalble.push(false);
                    }

                    this.setState({
                        rows: result.rows,
                        rowEnables:enalble,
                        key:Math.random()
                    });

                }
            }.bind(this)
        });
    }
    //addButton(value,index){
    //    console.log(value,index);
    //    let enables=this.state.rowEnables;
    //    enables[index]=true;
    //    //this.setState({
    //    //    rowEnables:enables,
    //    //    value: value
    //    //});
    //}
    saveParam(paraid,index){
        this.props.form.validateFields((errors, params) => {
            console.log(paraid,':sstt:',params[paraid]);
            util.httpRequest({
                type: "POST", url: 'param010202.json', params: {paraid:paraid,paravalue:params[paraid]}, success: function (result) {
                    if (result.ret_code == "0000") {
                        message.success(result.ret_msg);
                        let rowEnables=this.state.rowEnables;
                        rowEnables[index]=false;
                        this.setState({rowEnables:rowEnables});
                    }
                }.bind(this)
            });
        });
    }
    //123qw
    render() {
        const { getFieldProps, getFieldError, isFieldValidating} = this.props.form;
        let rows = [];

        const that = this;
        for(let index=0;index<this.state.rows.length;index++){
            console.log("重新生成表单",this.state.rows);
            let value = this.state.rows[index];
            const sindex = value.paraname.indexOf("${input}");
            const extra = sindex == -1 ? "" : value.paraname.substring(sindex + 8,value.paraname.length);
            console.log("extra",extra);
            let formItemLayout = {
                label: sindex == -1 ? value.paraname : value.paraname.substring(0, sindex),
                labelCol: {span: 8},
                wrapperCol: {span: 10}
            };
            const getFieldPropsCustom = (value, type, paraid, index) => {
                return {
                    initialValue: value,
                    onChange: function (val) {
                        if (this.state.rows[index].functype == "text") {
                            console.log("val:", val.target.value);
                            console.log(this.state.rows[index].paravalue);
                            if (val.target.value != this.state.rows[index].paravalue) {
                                console.log("增加保存按钮1")
                                let enables = this.state.rowEnables;
                                enables[index] = true;
                                this.setState({
                                    rowEnables: enables
                                });
                            }
                        } else if (this.state.rows[index].functype == "int" || this.state.rows[index].functype == "select") {
                            console.log("val:", val);
                            console.log(this.state.rows[index].paravalue);
                            if (val != this.state.rows[index].paravalue) {
                                console.log("增加保存按钮2")
                                let enables = this.state.rowEnables;
                                enables[index] = true;
                                this.setState({
                                    rowEnables: enables
                                });
                            }
                        }
                    }.bind(this),
                    rules: [
                        {type: type, required: true, message: "请输入该项"}
                    ]
                }
            };

            if (value.functype === "int") {
                rows.push(
                    <FormItem key={"FormItem" + index} {...formItemLayout}
                              help={isFieldValidating(value.paraid) ? '校验中...' : (getFieldError(value.paraid) || []).join(', ')}>
                        <InputNumber key={"InputNumber" + index} min={0} style={{width: "100%"}}
                                     {...getFieldProps(value.paraid, getFieldPropsCustom.bind(this, value.paravalue * 1, "number", value.paraid, index)())}></InputNumber>
                        {this.state.rowEnables[index] ?
                            <RightButton type="primary" styleName="parameter_save" btnTxt="保存" propsKey="431"
                                         oprate={this.saveParam.bind(this, value.paraid, index)}></RightButton> : null}
                        {extra ? <div className="parameter-extra"><span> {extra}</span></div> : ""}
                    </FormItem>);
            } else if (value.functype === "select") {
                rows.push(<FormItem {...formItemLayout} key={"FormItem" + value.paraid}
                                    help={isFieldValidating(value.paraid) ? '校验中...' : (getFieldError(value.paraid) || []).join(', ')}>
                    <Select
                        key={"Select" + value.paraid} {...getFieldProps(value.paraid, getFieldPropsCustom.bind(this, value.paravalue, "string", value.paraid, index)())}>
                        {this.createOptionsByDict(value.dict)}
                    </Select>
                    {this.state.rowEnables[index] ?
                        <RightButton type="primary" styleName="parameter_save" btnTxt="保存" propsKey="431"
                                     oprate={this.saveParam.bind(this, value.paraid, index)}></RightButton> : null}
                </FormItem>);
            } else if (value.functype === "text") {
                rows.push(
                    <FormItem {...formItemLayout} key={"FormItem" + index}
                              help={isFieldValidating(value.paraid) ? '校验中...' : (getFieldError(value.paraid) || []).join(', ')}>
                        <Input
                            key={"Input" + index} {...getFieldProps(value.paraid, getFieldPropsCustom.bind(this, value.paravalue, "string", value.paraid, index)())}></Input>
                        {this.state.rowEnables[index] ?
                            <RightButton type="primary" styleName="parameter_save" btnTxt="保存" propsKey="431"
                                         oprate={this.saveParam.bind(this, value.paraid, index)}></RightButton> : null}
                    </FormItem>);
            }
        }

        return (
            <Form horizontal >
                <Collapse onChange={this.callback.bind(this)} key={"collapsec" + this.props.index} className="m-g-10">
                    <Panel key={this.props.paraid} header={this.props.header} >
                        {rows}
                    </Panel>
                </Collapse>
            </Form>);
    }
}

export default InputPanel;